<?php
namespace backend\controllers;

use Yii;
use yii\web\Controller;
use yii\data\Pagination;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\models\Employees;
use backend\models\EmployeesForm;
use backend\models\EmployeesDataForm;
use backend\models\DepartureForm;
use common\models\Department;
use common\models\EmpDepart;
use backend\models\EmpDepartForm;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
use yii\web\ForbiddenHttpException;

/**
 * Employees controller
 */
class EmployeesController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['create', 'datalist', 'departure', 'distribute'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }

    /*
    * 新建员工
     */
    public function actionCreate()
    {
        if(!Yii::$app->user->can('createEmployee')){
            throw new ForbiddenHttpException('对不起，您现在还没获得此操作的权限');
        }

        if (isset(Yii::$app->request->get()['id']) && !empty(Yii::$app->request->get()['id'])) {
            $model = new EmployeesForm(Yii::$app->request->get()['id']);
        } else {
            $model = new EmployeesForm;
        }
        if (Yii::$app->request->isPost) {
            if ($model->load(Yii::$app->request->post()) && $model->validate()) {
                if ($model->addOrUpdate()) {
                    
                    return $this->redirect(['employees/datalist']);
                }
            }

            return $this->redirect(['employees/create']);
        }

        return $this->render('create', ['model' => $model]);
    }

    /*
    * 员工列表
     */
    public function actionDatalist()
    {
        if(!Yii::$app->user->can('updateEmployee')){
            throw new ForbiddenHttpException('对不起，您现在还没获得此操作的权限');
        }

        $data_form = new EmployeesDataForm;
        $data = Employees::find();
        if ($data_form->load(Yii::$app->request->get()) && $data_form->validate()) {
            $data = $data_form->query();
        }

        $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => '10']);
        $model = $data->offset($pages->offset)->limit($pages->limit)->all();
        $depart = Department::find()->select(['id', 'name'])->asArray()->all();
        $depart = ArrayHelper::map($depart, 'id', 'name');

        return $this->render('datalist', ['model' => $model, 'pages' => $pages, 'depart' => $depart]);
    }

    /*
    * 员工离职
     */
    public function actionDeparture()
    {
        $model = new DepartureForm;
        if ($model->load(Yii::$app->request->get()) && $model->validate()) {
            if ($model->departure()) {
                $re = [
                    'flag' => true,
                    'eid' => Yii::$app->request->get()['eid']
                ];
            } else {
                $re = [
                    'flag' => false,
                    'msg' => '修改失败'
                ];
            }
        } else {
            $re = [
                'flag' => false,
                'msg' => '数据加载失败'
            ];
        }

        return JSON::encode($re);
    }

    /*
    * 员工分配部门
     */
    public function actionDistribute($id)
    {
        $model = new EmpDepartForm;

        $emp_depart = EmpDepart::find()->where(['eid' => $id])->all();
        $depart = Department::find()->select(['id', 'name'])->asArray()->all();
        $depart = ArrayHelper::map($depart, 'id', 'name');
        $identity = [
            1 => '公司老总',
            2 => '部门经理',
            3 => '普通员工'
        ];
        $json_depart = JSON::encode($depart);
        $json_identity = JSON::encode($identity);
        $name = Employees::findOne($id)->realname;
        if ($model->load(Yii::$app->request->post())) {
            if ($model->update($id)) {
                
                return $this->redirect(['employees/datalist']);
            }
        }

        return $this->render('distribute', ['json_depart' => $json_depart, 'json_identity' => $json_identity, 'emp_depart' => $emp_depart, 'model' => $model,'depart' => $depart, 'identity' => $identity, 'id' => $id, 'name' => $name]);
    }


}